package com.bw.module_home.home.viewmodel

import com.bw.lib_common.base.BaseViewModel
import com.bw.lib_common.base.state.Failed
import com.bw.lib_common.base.state.Loading
import com.bw.lib_common.base.state.RequestEvent
import com.bw.lib_common.base.state.Success
import com.bw.module_home.R
import com.bw.module_home.home.model.HomeModel
import com.bw.module_home.home.model.HomeResult
import com.bw.module_home.home.repo.HomeRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepo): BaseViewModel() {

    val ServiceList= mutableListOf<HomeModel>()
    val noticeList= mutableListOf<HomeResult>()

    private val mNotice= MutableStateFlow<RequestEvent>(Loading)
    val noticeState get() =mNotice.asStateFlow()


    fun initList(){
        ServiceList.add(HomeModel(R.drawable.home_menu_image1,"车位管理"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image2,"访客登记"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image3,"物业报修"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image4,"考勤打卡"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image5,"房源信息"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image6,"商家推荐"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image7,"月卡会员"))
        ServiceList.add(HomeModel(R.drawable.home_menu_image8,"设备管理"))

    }

    fun getNotice(page:Int,size:Int){
        execute(
            action = repository.getNotice(page,size),
            onStart = {
                mNotice.value=Loading
            },
            onError = {
                mNotice.value= Failed(it)
            },
            onFinish = {
                noticeList.clear()
                noticeList.addAll(it.data)
                mNotice.value= Success
            }
        )
    }

}